// const WebSocket = require("ws");
const CryptoJS = require("crypto-js");
//加密
function encrypt(word, keyStr) {
  keyStr = keyStr ? keyStr : "XXXXXXXXXXXXX"; //判断是否存在ksy，不存在就用定义好的key
  var key = CryptoJS.enc.Utf8.parse(keyStr);
  var srcs = CryptoJS.enc.Utf8.parse(word);
  var encrypted = CryptoJS.AES.encrypt(srcs, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  });
  return encrypted.toString();
}
console.log(encrypt("1234256", "G7f9@vQ1!zT5w$P3Xj8#L2mN6bE4rA01"));
let data = encrypt("1234256", "G7f9@vQ1!zT5w$P3Xj8#L2mN6bE4rA01");
//解密
function decrypt(word, keyStr) {
  keyStr = keyStr ? keyStr : "XXXXXXXXXXXXX";
  var key = CryptoJS.enc.Utf8.parse(keyStr);
  var decrypt = CryptoJS.AES.decrypt(word, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  });
  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
console.log(decrypt(data, "G7f9@vQ1!zT5w$P3Xj8#L2mN6bE4rA01"));

// 连接到 WebSocket 服务器，使用自定义路径
// const ws = new WebSocket("ws://localhost/custom/path");

// ws.on("open", () => {
//   console.log("Connected to WebSocket server");
//   // 发送消息给服务器
//   ws.send("Hello from client");
// });

// // 处理收到的消息
// ws.on("message", (message) => {
//   console.log(`Received from server: ${message}，4444444444444444`);
// });

// // 处理连接关闭
// ws.on("close", () => {
//   console.log("Disconnected from WebSocket server");
// });

// // 处理连接错误
// ws.on("error", (error) => {
//   console.error(`WebSocket error: ${error}`);
// });
